<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Partes extends CI_Controller {

	function __construct() {
		parent::__construct();
		if(!$this->session->userdata('disproju')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}
	}
	public function index(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2','3','4'),$session_data);
		
		$p = new Parte();
		$p->select('id,nome_parte, cpfcnpj, UPPER(descricao)');
		$p->get();
		
		$data['parte'] = $p;
		$this->load->view('header');
		$this->load->view('v_parte_consulta',$data);
	}
	public function novo(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
	
		$this->load->view('header');
		$this->load->view('v_parte');
		
	}
	
	public function novomodal(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
		
		$this->load->view('v_parte');
		
	}
	
	public function salvar(){
	
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
		
		$p = new Parte();
		$p->cpfcnpj = $this->input->post('cpf_cnpj');
		$p->nome_parte =  strtoupper($this->input->post('nome_parte'));
		$p->descricao = strtoupper($this->input->post('descricao'));
		$p->cargo = strtoupper($this->input->post('cargo'));
		$p->telefone = $this->input->post('telefone');
		$p->endereco = strtoupper($this->input->post('endereco'));
		$p->cidade = strtoupper($this->input->post('cidade'));
		$p->estado = strtoupper($this->input->post('estado'));
		$p->tipopessoa = strtoupper($this->input->post('tipo'));
		
		
		//print_r($p);
		
		if($p->save()){
			$acao = $p->check_last_query(false,true);
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->dt_log = date('Y-m-d H:i:s');
			$log->save();
			echo 'OK';
		}else{
			echo $p->error->string;
		}
	
		
	}
	
	public function editar(){
	
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
			$id =  $this->input->post('id');
			$p = new Parte();
			$p->where('id', $id)->get();
			
			$p->cpfcnpj = $this->input->post('cpf_cnpj');
			$p->nome_parte =  strtoupper($this->input->post('nome_parte'));
			$p->descricao = strtoupper($this->input->post('descricao'));
			$p->cargo = strtoupper($this->input->post('cargo'));
			$p->telefone = $this->input->post('telefone');
			$p->endereco = strtoupper($this->input->post('endereco'));
			$p->cidade = strtoupper($this->input->post('cidade'));
			$p->estado = strtoupper($this->input->post('estado'));
			$p->tipopessoa = strtoupper($this->input->post('tipo'));
			

			if($p->save()){
				$acao = $p->check_last_query(false,true);
				$log = new Log();
				$log->ip = $this->session->userdata('ip_address');
				$log->usuario = $session_data["nome"];
				$log->acao = $acao;
				$log->dt_log = date('Y-m-d H:i:s');
				$log->save();
				echo 'OK';
			}else{
				$acao = $p->error->string;
				$log = new Log();
				$log->ip = $this->session->userdata('ip_address');
				$log->usuario = $session_data["nome"];
				$log->acao = $acao;
				$log->dt_log = date('Y-m-d H:i:s');
				$log->save();
				echo '<div class="danger alert-danger" role="danger">ERRO AO CADASTRAR!!</div>';
			}
		}
		
	}

	public function info($id = NULL){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2','3','4'),$session_data);
		
		if( ! empty($id))
		{
			$p = new Parte();
			$p->get_by_id($id);
			if( ! $p->exists())
			{
				show_error('Invalid User ID');
			}
		}

		$data['parte'] = $p;
		$data['id'] = $id;
		$data['session_data'] = $session_data;
		
		$this->load->view('header');
		$this->load->view('v_parte_info',$data);
		
	}

	public function consultaPartesJson(){
		$term = $_GET['q'];
		$aux = array();
		$dados = array();
		$p = new Parte();
		$p->like('nome_parte', $term);
		$partes = $p->get();
		
		foreach($partes as $parte){
			$aux["id"] = $parte->id;
			$aux["name"] = $parte->nome_parte;
			array_push($dados,$aux);
		}
		echo json_encode($dados);
		
	
	}
	public function consultaPartesJsonInfo(){
		
		$aux = array();
		$dados = array();
		$p = new Parte();
		$p->like('nome_parte', 'PRE');
		$partes = $p->get();
		
		foreach($partes as $parte){
			$aux['id'] = $parte->id;
			$aux['name'] = $parte->nome_parte;
			array_push($dados,$aux);
		}
		echo json_encode($dados);
		
	
	}
	public function nomePartes($num, $tipo){
		$msg = '';
		
		$processo = new Parte_Processo();
		
		$processo->where("processo_id",$num)
		->where("tipos_parte",$tipo)
		->include_related('parte', array('nome_parte'))
		->order_by("tipos_parte asc")
		->get();
		
		foreach($processo as $p) {
			$msg .= $p->parte_nome_parte." ";
		}
		return $msg;
	}
	
	public function validaCpfCnpj(){
		$cpfcnpj = $_GET['cpf_cnpj'];
		
		$p = new Parte();
		$p->get_by_cpfcnpj($cpfcnpj);
		if( ! $p->exists())
		{
			//return true;
			echo "true";
		}else{
			//return false;
			echo "false";
		}

	}
	public function historico(){
	
		$pp = new Parte_Processo();
		echo $pp->historico();
	
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */